package com.winner.data.util;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.winner.data.entity.User;

import java.util.*;
import java.util.stream.Collectors;

/**
 * hashmap的putall方法,去除map中已经包含的值 (map的key和value的字段附和条件就会去除) 而list一般用filter赋值给新的list, list转map, map转list,
 *
 * @author wangxinle5
 * @since 2023-09-14
 */
public class HashMapUtil {

    public static void main(String[] args) {

        List<User> userList = Arrays.asList(new User(1L, "张三"), new User(2L, "李四"));
        // list转map(如果需要返回hashmap, 则需要强转, 一般不需要,用map就可以了)
        Map<String, User> userMap = userList.stream().collect(Collectors.toMap(user -> user.getName(), user -> user));
        userMap.forEach((k, v) -> {
            System.out.println(k);
            System.out.println(v);
        });

        List<String> collect = userMap.keySet().stream().filter(k -> k.contains("张三")).collect(Collectors.toList());
        System.out.println(".keySet().stream()只能过滤出所有的值:" + collect);

        // 对map过滤得到需要的list
        List<User> result = userMap.entrySet().stream().filter(entry -> entry.getKey().equalsIgnoreCase("张三"))
            .map(Map.Entry::getValue).collect(Collectors.toList());
        System.out.println(StrUtil.format(".entrySet().stream() 对map的key和value的字段进行过滤得到的list结果是: {}", result));

        // map过滤后得到新的map
        Map<String, User> filterMap = userMap.entrySet().stream().filter(entry -> entry.getKey().equalsIgnoreCase("张三"))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));


        boolean flag = userMap.entrySet().stream().noneMatch(b -> b.getKey().equalsIgnoreCase("111"));

        // list转map key冲突时, 用新值覆盖旧值
        Map<String, User> userMap1 = userList.stream()
            .collect(Collectors.toMap(user -> user.getName(), user -> user, (existingValue, newValue) -> newValue));
        System.out.println(userMap1);

        // map再转为list
        ArrayList<User> userList1 = new ArrayList<>(userMap1.values());
        System.out.println(userList1);

        // 在使用 putAll 方法时，如果两个 Map 中有相同的键，新的值将替代旧的值。
        HashMap<String, Integer> map1 = new HashMap<>();
        map1.put("key1", 1);
        map1.put("key2", 2);

        HashMap<String, Integer> map2 = new HashMap<>();
        map2.put("key1", 10);
        map2.put("key3", 30);

        map1.putAll(map2);
        System.out.println(map1);

        // 去除map中已经包含的值 (map的key和value的字段附和条件就会去除)
        HashMap<String, User> map3 = new HashMap<>();
        map3.put("1", new User(1L, "aa"));
        map3.put("2", new User(2L, "bb"));

        // 转json统一去掉空格
        ArrayList<User> list = new ArrayList<>();
        list.add(new User(1L, null));
        list.add(new User(2L, "  WW  "));
        String jsonString = JSON.toJSONString(list, (ValueFilter)(object, name, value) -> {
            if (value instanceof String) {
                return StrUtil.trim((String)value); // ((String) value).trim()
            }
            return value;
        });
        System.out.println(jsonString);

    }
}
